package com.ruoyi.web.exams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.exams.domain.ExamOption;
import com.ruoyi.web.exams.domain.ExamQuestion;
import com.ruoyi.web.exams.dto.CreateOptionVo;
import com.ruoyi.web.exams.dto.CreateQuestionWithOption;
import com.ruoyi.web.exams.mapper.ExamOptionMapper;
import com.ruoyi.web.exams.mapper.ExamQuestionMapper;
import com.ruoyi.web.exams.service.IExamOptionService;
import com.ruoyi.web.exams.service.IExamQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题Service业务层处理
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements IExamQuestionService {
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private ExamOptionMapper examOptionMapper;
    @Autowired
    private IExamOptionService examOptionService;
    /**
     * 查询题
     *
     * @param id 题主键
     * @return 题
     */
    @Override
    public ExamQuestion selectExamQuestionById(Long id) {
        return examQuestionMapper.selectExamQuestionById(id);
    }

    /**
     * 查询题列表
     *
     * @param queryparam 题
     * @return 题
     */
    @Override
    public List<ExamQuestion> selectExamQuestionList(ExamQuestion queryparam) {
        List<ExamQuestion> questions =  examQuestionMapper.selectExamQuestionList(queryparam);
        List<Long> questionIds = questions.stream().map(ExamQuestion::getId).collect(Collectors.toList());
        List<ExamOption> examOptions = examOptionMapper.selectList(new LambdaQueryWrapper<ExamOption>()
                .in(ExamOption::getQuestionId, questionIds));
        for (ExamQuestion examQuestion : questions) {
            List<ExamOption> options = new ArrayList<>();
            for (ExamOption examOption : examOptions) {
                if (examOption.getQuestionId().longValue() == examQuestion.getId().longValue()) {
                    options.add(examOption);
                }
            }
            examQuestion.setOptions(options.stream().sorted().collect(Collectors.toList()));
        }
        return questions;
    }

    /**
     * 新增题
     *
     * @param examQuestion 题
     * @return 结果
     */
    @Override
    public int insertExamQuestion(ExamQuestion examQuestion) {
        examQuestion.setCreateTime(DateUtils.getNowDate());
        return examQuestionMapper.insertExamQuestion(examQuestion);
    }

    /**
     * 修改题
     *
     * @param examQuestion 题
     * @return 结果
     */
    @Override
    public int updateExamQuestion(ExamQuestion examQuestion) {
        examQuestion.setUpdateTime(DateUtils.getNowDate());
        return examQuestionMapper.updateExamQuestion(examQuestion);
    }

    /**
     * 批量删除题
     *
     * @param ids 需要删除的题主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionByIds(Long[] ids) {
        return examQuestionMapper.deleteExamQuestionByIds(ids);
    }

    /**
     * 删除题信息
     *
     * @param id 题主键
     * @return 结果
     */
    @Override
    public int deleteExamQuestionById(Long id) {
        return examQuestionMapper.deleteExamQuestionById(id);
    }

    @Transactional
    @Override
    public boolean createQuestion(CreateQuestionWithOption examQuestion) {
        ExamQuestion question = new ExamQuestion();
        BeanUtils.copyProperties(examQuestion, question);
        save(question);
        //删除之前的选项
        examOptionService.remove(new LambdaQueryWrapper<ExamOption>().eq(ExamOption::getQuestionId , examQuestion.getId()));
        List<ExamOption> options = new ArrayList<>();
        for (CreateOptionVo createOptionVo : examQuestion.getOptions()) {
            ExamOption examOption = new ExamOption();
            BeanUtils.copyProperties(createOptionVo, examOption);
            examOption.setQuestionId(question.getId());
            options.add(examOption);
        }
        return examOptionService.saveBatch(options);
    }
}